﻿using FOModels.ConsumerLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.ConsumerLoan
{
    public class ConsumerLoanDAL
    {
        private SQLDataAccess db;
        public ConsumerLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from ConsumerLoan order by ConsumerLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from ConsumerLoan sa join ConsumerLoanLang hl on sa.ConsumerLoanID = hl.ConsumerLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getConsumerLoanrelati(int CompanyID, int ConsumerLoanID, int langid)
        {
            return db.exeSelect("select * from ConsumerLoan ca join ConsumerLoanLang hl on ca.ConsumerLoanID = hl.ConsumerLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.ConsumerLoanID != " + ConsumerLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getConsumerLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from ConsumerLoan sa join ConsumerLoanLang hl on sa.ConsumerLoanID = hl.ConsumerLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableConsumerLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getConsumerLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from ConsumerLoan sa join ConsumerLoanLang hl on sa.ConsumerLoanID = hl.ConsumerLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableConsumerLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getConsumerLoanDefault(int langid)
        {
            return db.exeSelect("Select * from ConsumerLoan sa join ConsumerLoanLang hl on sa.ConsumerLoanID = hl.ConsumerLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableConsumerLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getConsumerLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from ConsumerLoan sa join ConsumerLoanLang hl on sa.ConsumerLoanID = hl.ConsumerLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableConsumerLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getConsumerLoanByID(int ConsumerLoanID, int langid)
        {
            return db.exeSelect("select * from ConsumerLoan ca join ConsumerLoanLang hl on ca.ConsumerLoanID = hl.ConsumerLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.ConsumerLoanID = " + ConsumerLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getConsumerLoanByIDManager(int ConsumerLoanID, int langid)
        {
            return db.exeSelect("select * from ConsumerLoan ca join ConsumerLoanLang hl on ca.ConsumerLoanID = hl.ConsumerLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.ConsumerLoanID = " + ConsumerLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdateConsumerLoan(ConsumerLoanModels models)
        {
            return db.exeUpdate("update ConsumerLoan set ConsumerLoanName = N'" + models.ConsumerLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where ConsumerLoanID = " + models.ConsumerLoanID + "");
        }
        public int InsertConsumerLoan(ConsumerLoanModels models)
        {
            return db.exeUpdate("insert into ConsumerLoan values (N'" + models.ConsumerLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeleteConsumerLoan(int ConsumerLoanID)
        {
            return db.exeUpdate("Delete from ConsumerLoan where ConsumerLoanID = " + ConsumerLoanID + "");
        }
        public int UpdateConsumerLoanLang(ConsumerLoanModels models, int langid)
        {
            return db.exeUpdate("update ConsumerLoanLang set ConsumerLoanName = N'" + models.ConsumerLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where ConsumerLoanID = " + models.ConsumerLoanID + " and LangID=" + langid + "");
        }
        public int InsertConsumerLoanLang(ConsumerLoanModels models, int langid)
        {
            return db.exeUpdate("insert into ConsumerLoanLang values (" + models.ConsumerLoanID + "," + langid + ",N'" + models.ConsumerLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeleteConsumerLoanLang(int ConsumerLoanID)
        {
            return db.exeUpdate("Delete from ConsumerLoanLang where ConsumerLoanID = " + ConsumerLoanID + "");
        }

        public int DeleteConsumerLoanApply(int ConsumerLoanApplyID)
        {
            return db.exeUpdate("Delete from ConsumerLoanApply where ConsumerLoanApplyID = " + ConsumerLoanApplyID + "");
        }
        public DataTable getcontractbyid(int ConsumerLoanapplyid)
        {
            return db.exeSelect("select * from ConsumerLoanApply ca join [ConsumerLoan] c on ca.ConsumerLoanID = c.ConsumerLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.ConsumerLoanApplyID = " + ConsumerLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from ConsumerLoanApply ca join [ConsumerLoan] c on ca.ConsumerLoanID = c.ConsumerLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from ConsumerLoanApply ca join [ConsumerLoan] c on ca.ConsumerLoanID = c.ConsumerLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int ConsumerLoanApply(int ConsumerLoanID, ConsumerLoanApplyModels app)
        {
            return db.exeUpdate("insert into ConsumerLoanApply(ConsumerLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + ConsumerLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(ConsumerLoanApplyModels ConsumerLoan)
        {
            return db.exeUpdate("update ConsumerLoanApply set StatusID= " + ConsumerLoan.StatusID + " where ConsumerLoanApplyID = " + ConsumerLoan.ConsumerLoanApplyID + "");
        }
    }
}
